using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace OpenMTS
{
    

    public partial class ProjectExplorer : ToolWindow
    {
        public enum NODE_ICON { PROJECT = 0, XML_FILE = 1, DIR_OPEN =3, DIR =4, RULESET = 5, REPORT = 6};
         

        private ProjectFile projectFile = null;

        public ProjectExplorer()
        {
            InitializeComponent();

        }

        private void ProjectExplorer_RightToLeftLayoutChanged(object sender, EventArgs e)
        {
            treeView1.RightToLeftLayout = RightToLeftLayout;
        }

        public void ProjectLoad(ProjectFile project)
        {
            closeProject();
            Project = project;
            //create the treeview root
            TreeNode treeRoot = new TreeNode(projectFile.Name, (int)NODE_ICON.PROJECT, (int)NODE_ICON.PROJECT);
            treeRoot.ContextMenuStrip = contextMenuStrip1;
            treeView1.Nodes.Add(treeRoot);

            XmlNode root = projectFile.ProjectRoot;
            fillTree(root, treeRoot);
        }

        public ProjectFile Project
        {
            get { return projectFile; }
            set
            {
                
                projectFile = value;
               
                
            }
        }

        public void closeProject()
        {
            projectFile = null;
            treeView1.Nodes.Clear();
        }

        //Recursively add project nodes to the view
        private void fillTree(XmlNode root, TreeNode treeRoot)
        {
            foreach (XmlNode n in root.ChildNodes)
            {
               TreeNode node1 = null;
                switch (n.Attributes["type"].Value)
                {
                    case "directory":
                        node1 = addDirNode(treeRoot, n);
                        break;
                    case "message":
                        node1 = addMessageNode(treeRoot, n);
                        break;
                    case "schema":
                        node1 = addSchemaNode(treeRoot, n);
                        break;
                    case "report":
                        node1 = addReportNode(treeRoot, n);
                        break;
                    case "ruleset":
                        node1 = addRulesetNode(treeRoot, n);
                        break;
                }
                if (n.HasChildNodes)
                    fillTree(n, node1);
            }
        }

        private TreeNode addDirNode(TreeNode parent, XmlNode node)
        {
            TreeNode n = addNode(parent, "directory", node.Attributes["name"].Value, 
                  (int)NODE_ICON.DIR, (int)NODE_ICON.DIR);
            n.Tag = node;

            return n;
        }

        private TreeNode addReportNode(TreeNode parent, XmlNode node)
        {
            TreeNode n = addNode(parent, "report", node.Attributes["name"].Value, (int)NODE_ICON.REPORT, (int)NODE_ICON.REPORT);
            n.Tag = node;

            return n;
        }

        private TreeNode addRulesetNode(TreeNode parent, XmlNode node)
        {
            TreeNode n = addNode(parent, "ruleset", node.Attributes["name"].Value, (int)NODE_ICON.RULESET, (int)NODE_ICON.RULESET);
            n.Tag = node;

            return n;
        }

        private TreeNode addSchemaNode(TreeNode parent, XmlNode node)
        {
            TreeNode n = addNode(parent, "schema", node.Attributes["name"].Value, (int)NODE_ICON.XML_FILE, (int)NODE_ICON.XML_FILE);
            n.Tag = node;

            return n;
        }

        private TreeNode addMessageNode(TreeNode parent, XmlNode node)
        {
            TreeNode n = addNode(parent, "message", node.Attributes["name"].Value, (int)NODE_ICON.XML_FILE, (int)NODE_ICON.XML_FILE);
            n.Tag = node;

            return n;
        }

        private TreeNode addNode(TreeNode parent, string sKey, string name, int image, int selImage)
        {
            return parent.Nodes.Add(sKey, name, image, selImage);

        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {

            TreeNode node = treeView1.SelectedNode;
            if (node != null && 
                (node.ImageIndex == (int)NODE_ICON.XML_FILE ||
                 node.ImageIndex == (int)NODE_ICON.RULESET ||
                 node.ImageIndex == (int)NODE_ICON.REPORT))
            {
                string sDir = projectFile.Location;
                if (node.Parent.ImageIndex == (int)NODE_ICON.DIR_OPEN)
                {
                    //TODO: This should be recursive in case we go to subdirs
                    sDir = string.Format("{0}{1}\\",sDir,node.Parent.Text);
                }

                //TODO: This does not work when window is not docked
                MainForm main = (MainForm)this.DockPanel.Parent;
                main.openDocument(sDir + node.Text);//not crazy about this
            }
        }

        private void schemaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = @"Schema files (*.xsd)|*.xsd|All files (*.*)|*.*";
            DialogResult result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                //copy the schema file to the schemas directory
                //add it to the project file and the treeview
                XmlNode rc = projectFile.addFile(dlg.FileName, ProjectFile.FILE_TYPE.schema);
                if (rc != null)
                {
                    TreeNode[] nodes = treeView1.Nodes.Find("directory", true);
                    foreach (TreeNode t in nodes)
                    {
                        if(t.Text == "schemas")
                        {
                            addSchemaNode(t, rc);
                            break;
                        }
                    }
                    
                }
               
            }
        }

        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            if (e.Node.ImageIndex == (int)NODE_ICON.DIR)
                e.Node.ImageIndex = e.Node.SelectedImageIndex = (int)NODE_ICON.DIR_OPEN;
        }

        private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
        {
            if (e.Node.ImageIndex == (int)NODE_ICON.DIR_OPEN)
                e.Node.ImageIndex = e.Node.SelectedImageIndex = (int)NODE_ICON.DIR;
        }

        private void rulesetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = @"Ruleset files (*.xml)|*.xml|All files (*.*)|*.*";
            DialogResult result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {
                
                //add it to the project file and the treeview
                XmlNode rc = projectFile.addFile(dlg.FileName, ProjectFile.FILE_TYPE.ruleset);
                if (rc != null)
                {
                    TreeNode[] nodes = treeView1.Nodes.Find("directory", true);
                    foreach (TreeNode t in nodes)
                    {
                        if (t.Text == "rulesets")
                        {
                            addRulesetNode(t, rc);
                            break;
                        }
                    }

                }

            }
        }

        private void messageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = @"Message files (*.xml)|*.xml|All files (*.*)|*.*";
            DialogResult result = dlg.ShowDialog();
            if (result == DialogResult.OK)
            {

                //add it to the project file and the treeview
                XmlNode rc = projectFile.addFile(dlg.FileName, ProjectFile.FILE_TYPE.message);
                if (rc != null)
                {
                   
                    addMessageNode(treeView1.TopNode, rc);
                  
                }

            }
        }
        
    }
}